//===============================================================================
// Microsoft patterns & practices
// Smart Client Software Factory 2010
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
//----------------------------------------------------------------------------------------
// patterns & practices - Smart Client Software Factory - Guidance Package
//
// This file was generated by the "Add View" recipe.
//
// This class is the concrete implementation of a View in the Model-View-Presenter 
// pattern. Communication between the Presenter and this class is acheived through 
// an interface to facilitate separation and testability.
// Note that the Presenter generated by the same recipe, will automatically be created
// by CAB through [CreateNew] and bidirectional references will be added.
//
// For more information see:
// ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.scsf.2006jun/SCSF/html/03-030-Model%20View%20Presenter%20%20MVP%20.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

using System;
using System.Globalization;
using System.Windows.Forms;
using GlobalBank.Infrastructure.Interface.BusinessEntities;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;

namespace GlobalBank.BranchSystems.Module.Views
{
	[SmartPart]
	public partial class CustomerSummaryView : UserControl, ICustomerSummaryView, ISmartPartInfoProvider
	{
		public CustomerSummaryView()
		{
			InitializeComponent();
		}


		/// <summary>
		/// Sets the presenter. The dependency injection system will automatically
		/// create a new presenter for you.
		/// </summary>
		[CreateNew]
		public CustomerSummaryViewPresenter Presenter
		{
			set
			{
				_presenter = value;
				_presenter.View = this;
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			_presenter.OnViewReady();
		}

		// ICustomerSummaryView implementation

		public void UpdateCustomerAccounts(Account[] accounts)
		{
			_accountBindingSource.DataSource = accounts;
		}

		public void UpdateCustomerCreditCards(CreditCard[] creditCards)
		{
			_creditCardBindingSource.DataSource = creditCards;
		}

		public void UpdateCustomerAlerts(Alert[] alerts)
		{
			_alertBindingSource.DataSource = alerts;
		}

		public void UpdateQueueInfo(QueueEntry queueEntry)
		{
			_timeInTextBox.Text = queueEntry.TimeIn.ToShortTimeString();
			_descriptionTextBox.Text = queueEntry.Description;
			_reasonCodeTextBox.Text = queueEntry.ReasonCode;
			_statusTextBox.Text = queueEntry.Status;
		}

		public void UpdateCustomerInfo(Person visitor)
		{
			_nameTextBox.Text =
				String.Format(CultureInfo.CurrentUICulture, "{0} {1} {2}", visitor.FirstName, visitor.MiddleInitial,
				              visitor.LastName);
			_ssnTextBox.Text = visitor.SocialSecurityNumber;
			if (visitor.Addresses != null && visitor.Addresses.Length > 0)
			{
				_addressTextBox.Text =
					String.Format(CultureInfo.CurrentUICulture, "{0}, {1}, {2} {3}", visitor.Addresses[0].Address1,
					              visitor.Addresses[0].City, visitor.Addresses[0].StateProvince, visitor.Addresses[0].PostalZipCode);
			}
			if (visitor.PhoneNumbers != null)
			{
				foreach (PhoneNumber phone in visitor.PhoneNumbers)
				{
					if (phone.PhoneType == PhoneType.Home)
					{
						_homeNumberTextBox.Text = phone.Number;
					}
				}
			}
			if (visitor.EmailAddresses != null && visitor.EmailAddresses.Length > 0)
			{
				_emailTextBox.Text = visitor.EmailAddresses[0].Address;
			}
		}

		// ISmartPartInfoProvider implementation

		public ISmartPartInfo GetSmartPartInfo(Type smartPartInfoType)
		{
			return new SmartPartInfo(Properties.Resources.CustomerSummaryViewTitle,
			                         Properties.Resources.CustomerSummaryViewDescription);
		}

		// Event handlers

		private void _serviceCompletedButton_Click(object sender, EventArgs e)
		{
			_presenter.ServiceComplete();
		}

		private void _creditCardDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			CreditCard selected = _creditCardBindingSource.Current as CreditCard;
			if (selected != null)
				_presenter.OpenCreditCard(selected);
		}
	}
}
